<template>
  <DaprPlusTable
    :table-title="$t('fsb.softwares.updateLog.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :form-options="formOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultUpdateLog"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script lang="ts" setup>
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import type { VbenFormProps } from '#/adapter/form';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import {
  createBooleanColumn,
  type IOutDto,
  type IInDto,
  type EntityInfo,
} from '#/utils/daprplus';
import { $t } from '#/locales';

const entity: EntityInfo = {
  code: 'UpdateLog',
  display: $t('fsb.softwares.updateLog.title'),
};

const baseUrl = '/softwares/api/updatelog';
const crudApi = createCrudApi<UpdateLogInDto, UpdateLogOutDto>(baseUrl);

// 配置搜索表单
const formOptions: VbenFormProps = {
  collapsed: false,
  schema: [
    {
      component: 'Input',
      label: '版本号',
      fieldName: 'version',
    },
    {
      component: 'Input',
      label: '标题',
      fieldName: 'title',
    },
    {
      component: 'VbenSelect',
      label: '更新类型',
      fieldName: 'updateType',
      componentProps: {
        options: [
          { label: '功能更新', value: 0 },
          { label: '问题修复', value: 1 },
          { label: '性能优化', value: 2 },
        ],
      },
    },
    {
      component: 'VbenSelect',
      label: '强制更新',
      fieldName: 'isForce',
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
    },
  ],
  showCollapseButton: true,
  submitOnChange: true,
};

// 验证规则
const validRules = {
  version: [
    { required: true, message: '版本号不能为空' },
    { max: 50, message: '版本号长度不能超过50个字符' },
  ],
  title: [
    { required: true, message: '标题不能为空' },
    { max: 200, message: '标题长度不能超过200个字符' },
  ],
  content: [{ required: true, message: '更新内容不能为空' }],
  updateType: [{ required: true, message: '更新类型不能为空' }],
};

// 扩展配置
const gridOptions: VxeGridProps<UpdateLogOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'version',
      title: '版本号',
      sortable: true,
      align: 'left',
      width: 120,
      editRender: { name: 'input' },
    },
    {
      field: 'title',
      title: '标题',
      align: 'left',
      minWidth: 200,
      editRender: { name: 'input' },
    },
    {
      field: 'content',
      title: '更新内容',
      align: 'left',
      minWidth: 300,
      editRender: { name: 'textarea' },
      showOverflow: true,
    },
    {
      field: 'updateType',
      title: '更新类型',
      width: 100,
      formatter: ({ cellValue }) => {
        const typeMap: Record<number, string> = {
          0: '功能更新',
          1: '问题修复',
          2: '性能优化',
        };
        return typeMap[cellValue as number] || cellValue;
      },
      editRender: {
        name: 'VxeSelect',
        props: {
          options: [
            { label: '功能更新', value: 0 },
            { label: '问题修复', value: 1 },
            { label: '性能优化', value: 2 },
          ],
        },
      },
    },
    createBooleanColumn('isForce', '强制更新', { width: 90 }),
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 150,
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultUpdateLog(): UpdateLogInDto {
  return {
    id: '0',
    version: '',
    title: '',
    content: '',
    updateType: 0,
    isForce: false,
  };
}

export interface UpdateLogOutDto extends IOutDto {
  /** 版本号 */
  version: string;
  /** 标题 */
  title: string;
  /** 更新内容 */
  content: string;
  /** 更新类型：0-功能更新，1-问题修复，2-性能优化 */
  updateType: number;
  /** 是否强制更新 */
  isForce: boolean;
  /** 创建时间 */
  createdTime: string;
}

export interface UpdateLogInDto extends IInDto {
  /** 版本号 */
  version: string;
  /** 标题 */
  title: string;
  /** 更新内容 */
  content: string;
  /** 更新类型：0-功能更新，1-问题修复，2-性能优化 */
  updateType: number;
  /** 是否强制更新 */
  isForce: boolean;
}
</script>

<style scoped></style>
